package com.pengtu.entity.app;

import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.NotEmpty;
import org.jbpm.api.identity.Group;
import org.jbpm.api.task.Participation;
import org.springframework.security.core.GrantedAuthority;

import com.google.common.collect.Sets;
import com.pengtu.entity.IdEntity;
import com.pengtu.utils.reflection.ConvertUtils;

/**
 * 角色.
 * 
 * 注释见{@link User}.
 * 
 * @author calvin
 */
@Entity
@Table(name = "T_APP_ROLE")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Role extends IdEntity implements GrantedAuthority,Group{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8532109958070142630L;
	private String roleName;
	private String roleDesc;
	private String remark;
	private String category;
	private Set<Authority> authorityList = Sets.newHashSet();
	
	public Role() {

	}
	
	public Role(String id, String name) {
		this.id = id;
		this.roleName = name;
	}
	
	@NotEmpty(message="角色名称不能为空")
	@Column(nullable = false,unique = true,length = 50,name="ROLENAME")
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	
	@Column(length = 200,name="ROLEDESC")
	public String getRoleDesc() {
		return roleDesc;
	}
	public void setRoleDesc(String roleDesc) {
		this.roleDesc = roleDesc;
	}
	
	@Column(length = 400)
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	@ManyToMany
	@JoinTable(name="R_APP_ROLEAUTHORITY",joinColumns={@JoinColumn(name="ROLEID")},inverseJoinColumns = {@JoinColumn(name="AUTID")})
	@Fetch(FetchMode.SUBSELECT)
	@OrderBy("id")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public Set<Authority> getAuthorityList() {
		return authorityList;
	}
	public void setAuthorityList(Set<Authority> authorityList) {
		this.authorityList = authorityList;
	}
	
	@Transient
	public String getAuthNames() {
		return ConvertUtils.convertElementPropertyToString(authorityList, "autName", ", ");
	}

	@Transient
	@SuppressWarnings("unchecked")
	public List<String> getAuthIds() {
		return ConvertUtils.convertElementPropertyToList(authorityList, "id");
	}

	@Transient
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
	
	@Transient
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return roleName;
	}

	@Transient
	@Override
	public String getType() {
		// TODO Auto-generated method stub
		return Participation.CANDIDATE;
	}

	@Transient
	@Override
	public String getAuthority() {
		// TODO Auto-generated method stub
		return roleName;
	}

	@Column(name="CATEGORY")
	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}
	
	
	
	
	
}
